<?php

require_once 'SpecHelper.php';

class DescribeRunner extends PHPSpec_Context
{

	public function itShouldLoadAListOfImplementationFiles()
	{
		$mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');
        $mutagen->prepare();
		$files = $mutagen->getFiles();
		$this->spec(preg_match("/a\.php$/", $files[0]))->should->be(1); // need a feckin' pcre matcher!!!
    }

	public function itShouldCreateAWorkingBaseDirectory()
    {
		$mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');
        $mutagen->prepare();
		$this->spec(is_dir('./.wdir'))->should->beTrue();
    }

	public function itShouldCreateAWorkingCopyOfImplementationFilesInWdir()
	{
		$mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');
        $mutagen->prepare();
		$this->spec(file_exists('./.wdir/src_1/a.php'))->should->beTrue();
	}
	
	public function itShouldDeleteWorkingCopiesAfterMutationExercise()
	{
	    $mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');

        $stub = new StubGenerator1;
        $mutagen->setGenerator($stub);
	    $mutagen->execute();

	    $this->spec(file_exists('./.wdir/src_1/a.php'))->should->beFalse();
	}

	public function itShouldDeleteWorkingDirectoriesAfterMutationExercise()
	{
		if (php_uname('s') == 'Windows NT') {
            $this->pending('Skipped on Windows XP');
        }
		$mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');

        $stub = new StubGenerator1;
        $mutagen->setGenerator($stub);
	    $mutagen->execute();

	    $this->spec(file_exists('./.wdir'))->should->beFalse();
	}

    public function itShouldGenerateMutablesWhenExecuted() 
    {
        $mutagen = new PHPMutagen_Runner();
        $mutagen->setBaseDirectory('./_files');
        $mutagen->setWorkingDirectory('./.wdir');
        $mutagen->setSourceDirectory('./.wdir/src_1');

        $stub = new StubGenerator1;
        $mutagen->setGenerator($stub);
        $mutagen->execute();

        $this->spec($mutagen->getMutables())->should->be(array());
    }

	public function after()
	{
	    $this->_deleteDirectoryRecursive('./.wdir');
	}
	
	protected function _deleteDirectoryRecursive($target)
    {
        if (!file_exists($target)) {
        	return;
        }
        
        $d = dir($target);
        while (FALSE !== ($res = $d->read())) {
            if ($res == '.' || $res == '..') {
                continue;
            }
            $entry = $target . '/' . $res;           
            if (is_dir($entry)) {
                $this->_deleteDirectoryRecursive($entry);
                continue;
            }
            unlink($entry);
        }
        if ($_ENV['OS'] !== 'Windows_NT') {
			rmdir($target);
		}
        $d->close();
    }

}

/** Stubs until I get PHPMock ;) **/

class StubGenerator1 extends PHPMutagen_Generator
{
    public function __construct() 
    {
    }
    public function generate() 
    {
        return array();
    }
}